﻿@{
    ViewBag.Title = "Treasure - Websites";
}
@using treasure.NET.Models;
@using PagedList.Mvc;
@using PagedList;

@using (Html.BeginForm("Index", "Websites", FormMethod.Get))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            <div class="col-md-10">
                <input class="form-control text-box single-line" id="key" placeholder="Search KEY" name="key" type="text" value="@ViewBag.key">
            </div>
        </div>
    </div>
}

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            分类
        </th>
        <th>
            图标
        </th>
        <th>
            链接
        </th>
        <th>
            描述
        </th>
        <th>
            浏览量
        </th>

        <th></th>
    </tr>

    @foreach (var item in ViewBag.OnePageOfWebsites as IPagedList<website>)
    {
        if (!string.IsNullOrEmpty(item.desc) && item.desc.Length > 20)
        {
            item.desc = item.desc.Substring(0, 20);
        }

        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.category.name)
            </td>
            <td>
                <img style="height:30px;width:auto;" src="@Html.DisplayFor(modelItem => item.faviconurl)" />
            </td>
            <td>
                <a target="_blank" class="url" href="@item.url" data-id="@item.id">@item.title</a>
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.desc)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.pageview)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.id }) |
                @Html.ActionLink("Details", "Details", new { id = item.id }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.id })
            </td>
        </tr>
    }

</table>
@Html.PagedListPager((IPagedList)ViewBag.OnePageOfWebsites, page => Url.Action("Index", new { page, key = ViewBag.key }))

@section scripts{
    <script>
        $(function () {
            $('.url').click(function () {
                var id = $(this).attr('data-id');
                $.ajax({
                    url: '/websites/jump',
                    type: 'post',
                    dataType: 'json',
                    data: { id: id },
                    success: function () {
                        window.location.reload();
                    }
                });
            });
        })

    </script>
}